﻿using Microsoft.EntityFrameworkCore;

namespace Intro
{

    /// <summary>
    ///   图书数据库上下文类
    /// </summary>

    public class BooksContext : DbContext
    {
        //连接字符串。本例中连接字符串是硬编码
        private const string ConnectionString = @"server=(localdb)\MSSQLLocalDb;database=Books;trusted_connection=true";

        //DbSet <TEntity>可以用来查询和保存的情况TEntity
        //针对DbSet <TEntity>的 LINQ查询将转换为针对数据库的查询。
        //本例使用定义Books为DbSet<Book>类型实例，针对Books的Linq查询则会转化为对Books数据库表的查询
        public DbSet<Book> Books { get; set; }
       
        //
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            //将上下文映射到UseSqlServer数据库
            optionsBuilder.UseSqlServer(ConnectionString);

        }
    }
}
